package cn.rsa.aiwangren.config;

import cn.rsa.aiwangren.entity.KeyPairs;
import com.alibaba.fastjson.JSON;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * 密钥配置
 *
 * @author xiangtianlei
 * @date 2024/04/09
 */
@ConfigurationProperties(prefix = "rsa-encrypt")
@Component
public class SecretKeyConfig {

    private String charset = "UTF-8";

    private boolean open = true;

    private boolean showLog = false;
    private int keySize = 2048;

    /**
     * RSA Maximum Encrypted Plaintext Size
     * keySize / 8 - 11
     */
    private int MAX_ENCRYPT_BLOCK = keySize / 8 - 11;

    /**
     * RSA Maximum decrypted ciphertext size
     * keySize / 8
     */
    private int MAX_DECRYPT_BLOCK = keySize / 8;

    private Map<String, KeyPairs> keyPairs;


    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    public boolean isShowLog() {
        return showLog;
    }

    public void setShowLog(boolean showLog) {
        this.showLog = showLog;
    }

    public int getKeySize() {
        return keySize;
    }

    public void setKeySize(int keySize) {
        this.keySize = keySize;
    }

    public int getMAX_ENCRYPT_BLOCK() {
        return MAX_ENCRYPT_BLOCK;
    }

    public void setMAX_ENCRYPT_BLOCK(int MAX_ENCRYPT_BLOCK) {
        this.MAX_ENCRYPT_BLOCK = MAX_ENCRYPT_BLOCK;
    }

    public int getMAX_DECRYPT_BLOCK() {
        return MAX_DECRYPT_BLOCK;
    }

    public void setMAX_DECRYPT_BLOCK(int MAX_DECRYPT_BLOCK) {
        this.MAX_DECRYPT_BLOCK = MAX_DECRYPT_BLOCK;
    }

    public void setKeyPairs(Map<String, KeyPairs> keyPairs) {
        this.keyPairs = keyPairs;
    }

    public KeyPairs getKeyPair(String channel) {
        return keyPairs.get(channel);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
